package maptool

import (
	"fmt"
	"hft/global"
	"io/ioutil"
	"net/http"
	"net/url"
)

type BaiduMap struct{}

func NewBaiduMapApi(uri string, q string, city string) ([]byte, error) {
	ak := global.OC_CONFIG.BaiduMap.Key
	host := global.OC_CONFIG.BaiduMap.Url
	fmt.Println(111, q)
	params := url.Values{
		"query":      []string{q},
		"region":     []string{city},
		"city_limit": []string{"true"},
		"output":     []string{"json"},
		"ak":         []string{ak},
	}

	request, err := url.Parse(host + uri + "?" + params.Encode())

	if nil != err {
		fmt.Printf("host error: %v", err)
		return nil, err
	}

	resp, err1 := http.Get(request.String())
	fmt.Printf("url: %s\n", request.String())
	defer resp.Body.Close()
	if err1 != nil {
		fmt.Printf("request error: %v", err1)
		return nil, err
	}
	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		fmt.Printf("response error: %v", err2)
	}

	return body, nil
}
